package com.bst.web.etl;

import com.bst.common.annotation.Log;
import com.bst.common.core.controller.BaseController;
import com.bst.common.core.domain.AjaxResult;
import com.bst.common.core.domain.Ztree;
import com.bst.common.core.domain.entity.SysDept;
import com.bst.common.core.page.TableDataInfo;
import com.bst.common.enums.BusinessType;
import com.bst.common.exception.base.BaseException;
import com.bst.common.utils.StringUtils;
import com.bst.etl.domain.EtlDrawDm;
import com.bst.etl.domain.EtlDrawDw;
import com.bst.etl.service.IEtlDrawDmService;
import com.bst.etl.service.IEtlDrawDwService;
import com.bst.md.domain.MdIndex;
import com.bst.md.domain.MdMedPubfld;
import com.bst.md.domain.MdMedTb;
import com.bst.md.domain.MdMedTbFld;
import com.bst.md.service.IMdIndexService;
import com.bst.md.service.IMdMedPubfldService;
import com.bst.md.service.IMdMedTbFldService;
import com.bst.md.service.IMdMedTbService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 数据集市Controller
 * 
 * @author ruoyi
 * @date 2022-07-17
 */
@Controller
@RequestMapping("/base/etl/dm")
public class EtlDrawDmController extends BaseController
{
    private String prefix = "etl/dm";

    @Autowired
    private IEtlDrawDmService etlDrawDmService;
    @Autowired
    IEtlDrawDwService etlDrawDwService;
    @Autowired
    IMdMedTbFldService mdMedTbFldService;
    @Autowired
    IMdIndexService mdIndexService;
    @Autowired
    IMdMedTbService mdMedTbService;
    @Autowired
    IMdMedPubfldService mdMedPubfldService;

    @GetMapping()
    public String dm()
    {
        return prefix + "/dm";
    }

    /**
     * 查询数据集市列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EtlDrawDm etlDrawDm)
    {
        startPage();
        MdIndex index = new MdIndex();
        index.setNa(etlDrawDm.getNaIndex());
        index.setSdSys(etlDrawDm.getSdSys());
        List<EtlDrawDm> list = etlDrawDmService.selectEtlDrawDmByIndex(index);
        return getDataTable(list);
    }

    @PostMapping("/list2")
    @ResponseBody
    public AjaxResult list2(EtlDrawDm etlDrawDm)
    {
        MdIndex index = new MdIndex();
        index.setNa(etlDrawDm.getNaIndex());
        index.setSdSys(etlDrawDm.getSdSys());
        List<EtlDrawDm> list = etlDrawDmService.selectEtlDrawDmByIndex(index);
        return AjaxResult.success(list);
    }

    /**
     * 新增数据集市
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存数据集市
     */
    @Log(title = "数据集市", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EtlDrawDm etlDrawDm)
    {
        return toAjax(etlDrawDmService.insertEtlDrawDm(etlDrawDm));
    }

    @GetMapping("/saveByIdIndexs/{idIndexs}")
    @ResponseBody
    public AjaxResult saveByIdIndexs(@PathVariable("idIndexs") String idIndexs) {
        EtlDrawDm drawDm;
        for(String idIndex:idIndexs.split(",")) {
            MdIndex index = mdIndexService.selectMdIndexByIdIndex(idIndex);
            drawDm = new EtlDrawDm();
            drawDm.setIdIndex(idIndex);
            List<EtlDrawDm> drawDms = etlDrawDmService.selectEtlDrawDmList(drawDm);
            if(drawDms.size()==0) {
                drawDm.setIdIndex(idIndex);
                drawDm.setNaIndex(index.getNa());
                if(index.getFgAccu()==1 && index.getEuTp()!=2) {
                    drawDm.setFgTb(1);
                } else {
                    drawDm.setFgTb(0);
                }
                drawDm.setSdSys(index.getSdSys());
                etlDrawDmService.insertEtlDrawDm(drawDm);
            }
        }
        return AjaxResult.success("成功");
    }

    /**
     * 修改数据集市
     */
    @GetMapping("/edit/{idDrawDm}")
    public String edit(@PathVariable("idDrawDm") Long idDrawDm, ModelMap mmap)
    {
        EtlDrawDm etlDrawDm = etlDrawDmService.selectEtlDrawDmByIdDrawDm(idDrawDm);
        mmap.put("etlDrawDm", etlDrawDm);
        MdIndex index = mdIndexService.selectMdIndexByIdIndex(etlDrawDm.getIdIndex());
        List<MdMedTb> relyList = etlDrawDmService.selectRelys(idDrawDm);
        mmap.put("relyList", relyList);
        String flds = etlDrawDm.getFlds();
        List<MdMedTbFld> fldList = new ArrayList<>();
        if(StringUtils.isNotEmpty(flds)) {
            List<MdMedPubfld> pubFldList = mdMedPubfldService.findByIdList(Arrays.asList(flds.split(",")));
            MdMedTbFld tbFld;
            for (MdMedPubfld mdMedPubfld : pubFldList) {
                tbFld = new MdMedTbFld();
                tbFld.setIdPubfld(mdMedPubfld.getIdPubfld());
                tbFld.setNaPubfld(mdMedPubfld.getNa());
                fldList.add(tbFld);
            }
        }
        mmap.put("fldList", fldList);
        mmap.put("index", index);
        return prefix + "/edit";
    }

    @GetMapping("/findCommonDims")
    @ResponseBody
    public TableDataInfo queryDmFldsByIdIndexs(String idIndexs) {
        List<MdMedPubfld> flds = etlDrawDmService.selectDmFldsByIdIndexs(idIndexs);
        return getDataTable(flds);
    }

    /**
     * 修改保存数据集市
     */
    @Log(title = "数据集市", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EtlDrawDm etlDrawDm)
    {
        return toAjax(etlDrawDmService.updateEtlDrawDm(etlDrawDm));
    }

    @GetMapping("/toAddRely/{idDrawDm}")
    public String toAddRely(@PathVariable("idDrawDm") Long idDrawDm, ModelMap mmap)
    {
        mmap.put("list", etlDrawDmService.selectNotRelyList(idDrawDm));
        mmap.put("dm",etlDrawDmService.selectEtlDrawDmByIdDrawDm(idDrawDm));
        return prefix + "/rely";
    }

    @GetMapping("/toAddFld/{idDrawDm}")
    public String toAddFld(@PathVariable("idDrawDm") Long idDrawDm, ModelMap mmap) {
        List<MdMedTbFld> flds = etlDrawDmService.selectRelyFlds(idDrawDm);
        mmap.put("flds",flds);mmap.put("dm",etlDrawDmService.selectEtlDrawDmByIdDrawDm(idDrawDm));
        return prefix+"/fld";
    }

    @GetMapping("/deleteRely")
    @ResponseBody
    public AjaxResult deleteRely(Long idDrawDm,String tableName) {
        etlDrawDmService.deleteRelyTable(tableName,idDrawDm);
        return AjaxResult.success("成功");
    }

    @GetMapping("/deleteFld")
    @ResponseBody
    public AjaxResult deleteFld(Long idDrawDm,String fld) {
        etlDrawDmService.deleteRelyFlds(fld,idDrawDm);
        return AjaxResult.success("成功");
    }

    @ApiOperation(value="创建DM表")
    @RequestMapping(value="/createTable/{idDrawDm}", method=RequestMethod.GET)
    @ResponseBody
    public AjaxResult createTable(@PathVariable("idDrawDm") Long idDrawDm) throws BaseException {
        AjaxResult result = etlDrawDmService.createDmTable(idDrawDm);
        return result;
    }

    @GetMapping("/selectByTemplateId")
    @ResponseBody
    public TableDataInfo selectByTemplateId (Integer templateId) {
        List<EtlDrawDm> list = etlDrawDmService.selectEtlDrawDmByIdTemplate(templateId);
        return getDataTable(list);
    }

    /**
     * 删除标准仓库
     */
    @RequiresPermissions("etl:dw:remove")
    @Log(title = "标准仓库", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(etlDrawDmService.deleteEtlDrawDmByIdDrawDms(ids));
    }

    @GetMapping("/tree")
    public String indexTree() {
        return prefix+"/tree_dm";
    }

    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData(String name)
    {
        SysDept dept = new SysDept();
        dept.setDeptName(name);
        List<Ztree> ztrees = etlDrawDmService.selectRootIndex();
        return ztrees;
    }
}
